﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using NUnit.Framework.SyntaxHelpers;
using SimpleExpressionEvaluator.Evaluation;
using SimpleExpressionEvaluator.Parsers.Irony;

namespace SimpleExpressionEvaluator.Tests.Compilation.Functions.AggregateFunctions
{
    [TestFixture]
    public class AggregateFunctionTestFixture
    {
        [Test]
        public void Will_Aggregate_Simple_List()
        {
            const string eStr = "Sum()";
            var compiler = new SimpleExpressionLanguageCompiler();
            var expr = compiler.CompileExpression<double>(eStr);
            Assert.That(expr.Evaluate(new ExpressionContext(new[] { 1, 2, 3, 4, 5 })), Is.EqualTo(15));
        }

        [Test]
        public void Will_Aggregate_Simple_List_With_Child_Expression()
        {
            const string eStr = "Sum(nothing,length)";
            var compiler = new SimpleExpressionLanguageCompiler();
            var expr = compiler.CompileExpression<double>(eStr);
            var data = new List<string> { "a", "ab", "abc", "abcd" };
            Assert.That(expr.Evaluate(new ExpressionContext(data)), Is.EqualTo(10));

        }

        [Test]
        public void Will_Evaluate_Context_Expression_And_Aggregate_List_Via_Expression()
        {
            const string eStr = "sum(strings,length)";
            var compiler = new SimpleExpressionLanguageCompiler();
            var expr = compiler.CompileExpression<double>(eStr);
            var data = new TestObject { Strings = new List<string> { "a", "ab", "abc", "abcd" } };
            Assert.That(expr.Evaluate(new ExpressionContext(data)), Is.EqualTo(10));
        }

        public class TestObject
        {
            public List<string> Strings { get; set; }
        }
    }
}
